package 注解;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author:谢君臣
 * @Date:2021/8/9-16:55
 * @version:1.0
 * @function:
 */
public class Test反射 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchFieldException {
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        Class<?> aClass = Class.forName("基础.注解.Test注解");
        Test注解 b1 = (Test注解) aClass.newInstance();  // 调用无参构造器
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor();
        Test注解 b2 = (Test注解) declaredConstructor.newInstance();
        Method setValue = aClass.getMethod("setValue", int.class);
//        System.out.println(b1.getValue());
        setValue.invoke(b2,10);
//        System.out.println(b2.getValue());
        Field value = aClass.getDeclaredField("value");
        value.setAccessible(true);  //取消权限
        value.set(b2,20);
//        System.out.println(b2.getValue());



    }
}
